package com.solo.quartz.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.solo.common.core.global.R;
import com.solo.common.logger.annotation.Logger;
import com.solo.common.logger.enums.LoggerType;
import com.solo.common.orm.core.query.Wrappers;
import com.solo.quartz.api.entity.QrtzJobLog;
import com.solo.quartz.model.job.log.QrtzJobLogConvert;
import com.solo.quartz.model.job.log.req.JobLogCreateReq;
import com.solo.quartz.model.job.log.req.JobLogQueryReq;
import com.solo.quartz.model.job.log.req.JobLogUpdateReq;
import com.solo.quartz.model.job.log.resp.JobLogGetResp;
import com.solo.quartz.model.job.log.resp.JobLogListResp;
import com.solo.quartz.service.QrtzJobLogService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 定时任务日志控制器
 * @author 十一
 * @since 2024-08-19 15:29
 * 人生若只如初见，何事秋风悲画扇
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/quartz/job-log")
public class QrtzJobLogController {

    private final QrtzJobLogService qrtzJobLogService;

    /**
     * 新增定时任务日志
     * @param req 定时任务日志新增对象
     * @return 响应信息
     */
    @PostMapping
    @SaCheckPermission("quartz-job-log-create")
    @Logger(value = "新增定时任务日志", type = LoggerType.CREATE)
    public R<Boolean> create(@Valid @RequestBody JobLogCreateReq req) {
        QrtzJobLog entity = QrtzJobLogConvert.INSTANCE.convert(req);
        return R.success(qrtzJobLogService.save(entity));
    }

    /**
     * 删除定时任务日志
     * @param jobLogIds 定时任务日志集合
     * @return 响应信息
     */
    @DeleteMapping("/{jobLogIds}")
    @SaCheckPermission("quartz-job-log-delete")
    @Logger(value = "删除定时任务日志", type = LoggerType.DELETE)
    public R<Boolean> delete(@PathVariable Long[] jobLogIds) {
        return R.success(qrtzJobLogService.removeByIds(Arrays.asList(jobLogIds)));
    }

    /**
     * 更新定时任务日志
     * @param req 定时任务日志更新对象
     * @return 响应信息
     */
    @PutMapping
    @SaCheckPermission("quartz-job-log-update")
    @Logger(value = "更新定时任务日志", type = LoggerType.UPDATE)
    public R<Boolean> update(@Valid @RequestBody JobLogUpdateReq req) {
        QrtzJobLog entity = QrtzJobLogConvert.INSTANCE.convert(req);
        return R.success(qrtzJobLogService.updateById(entity));
    }

    /**
     * 获取定时任务日志
     * @param jobLogId 定时任务日志
     * @return 响应信息
     */
    @GetMapping("/{jobLogId}")
    @SaCheckPermission("quartz-job-log-query")
    public R<JobLogGetResp> get(@PathVariable Long jobLogId) {
        return R.success(QrtzJobLogConvert.INSTANCE.convertGet(qrtzJobLogService.getById(jobLogId)));
    }

    /**
     * 分页查询定时任务日志列表
     * @param page 分页对象
     * @param req 定时任务日志查询对象
     * @return 响应信息
     */
    @GetMapping("/page")
    @SaCheckPermission("quartz-job-log-query")
    public R<Page<JobLogListResp>> page(Page<JobLogListResp> page, JobLogQueryReq req) {
        Page<JobLogListResp> list = qrtzJobLogService.pageAs(page, Wrappers.builder(req), JobLogListResp.class);
        return R.success(list);
    }

}
